package org.itboys.mobile.util;

import org.apache.commons.lang3.StringUtils;
import org.itboys.commons.utils.servlet.ServletContextHolder ;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dto.SundayMemberSessionDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;

/**
 * 通过accountId获取某些重要值
 * Created by Administrator on 2017/3/6.
 */
public class AccountIdHoldUtils {

    protected static Logger logger = LoggerFactory.getLogger(AccountIdHoldUtils.class);

    /**
     * 获取memberId
     * @return
     */
    public static Long getMemberId(){

        String accountId ="";
        try {
                accountId=ServletContextHolder.getRequest().getHeader(SundayCommonConstants.ACCOUNT_ID);
                if(StringUtils.isNotEmpty(accountId)){
                    logger.info("请求头中的accountId="+ accountId);
                    return Long.valueOf(accountId);
                }
                logger.error("错误：！！！！！！！请求头无accountId");
                //  ServletContextHolder.getRequest().getParameter()
                if(StringUtils.isEmpty(accountId))return 9L;//临时代码。上线后清除
            }catch (Exception e){
                logger.error("请求头中的accountId异常");
        }
        return 0l;


        //从缓存中 获取token
       // TokenRedisManager tokenRedisManager = SpringContextHolder.getBean("tokenRedisManager");
       // Token token = tokenRedisManager.getTokenByAccountId(accountId);
        //logger.info("Utils中的Token="+ JSON.toJSONString(token));
        //if(token != null){
          //  return token.getMemberId();
       // }else {
         //   return null;
            /*ResponseMessage.headError();
            throw new NullPointerException();*/
     //   }
    }

    /**
     * 从session中获取用户信息
     * @param request
     * @return
     */
    public static SundayMemberSessionDto getSessionMember(HttpServletRequest request){
        Object object = request.getSession().getAttribute("member");
        if(object==null){
            /*SundayMemberSessionDto dto = new SundayMemberSessionDto();
            dto.setId(1L);*/
            return null;
        }
        return (SundayMemberSessionDto)object;
    }
}
